<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加banner</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/layui.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('/layuiadmin/style/admin.css')}}" media="all">
    <link rel="stylesheet" href="{{asset('/layuiadmin/layui/css/formSelects-v4.css')}}" media="all">
    <style>
        .layui-card-header{width:80px;text-align: right;float:left;}
        .layui-card-body{margin-left:28px;}
        /*.layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}*/
        .up{position: relative;display: inline-block;cursor: pointer;border-color: #1ab394; color: #FFF;width: 92px !important;font-size: 10px !important;text-align: center !important;}
        .up input{position: absolute;top:0;left: 0;display: block;opacity: .01;width: 100px;height:30px;}
        .layui-upload-list{overflow: hidden;}
        input::-webkit-outer-spin-button,
        input::-webkit-inner-spin-button {-webkit-appearance: none !important;margin: 0;}
        .layui-upload{width:100%;}
        #demo1{width:100%;}
        .layui-card{box-shadow:none;}
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card" style="margin-top:0px">
        <div class="layui-card-header">添加apk</div>
        <div class="layui-card-body" style="padding: 15px;">
            <div class="layui-form" lay-filter="component-form-group">
{{--                <div class="layui-form-item school" style="width:500px">--}}
{{--                    <label class="layui-form-label" style="text-align:center">类型</label>--}}
{{--                    <div class="layui-input-block">--}}
{{--                        <input type="text" name="schoolname" lay-verify="schoolname" autocomplete="off" placeholder="请输入类型" class="layui-input type">--}}
{{--                    </div>--}}
{{--                </div>--}}
                <div class="layui-form-item" style="width:500px">
                    <label class="layui-form-label" style="text-align:center">类型</label>
                    <div class="layui-input-block">
                        <select name="type" id="type" lay-filter="type">
                            <option value="">选择设备类型</option>
                            <option value="face_TZH-L1">青蛙L1</option>
                            <option value="s_bf_qr68">百富qr68</option>
                            <option value="face_f4">蜻蜓F4</option>
                            <option value="face_pro">青蛙Pro</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-card-header" style="text-align:center">apk包</div>
                    <div class="layui-card">
                        <div class="layui-card-body" style="display: inline-block;padding:0;margin-left:0;width:391px;">
                            <input type="text" placeholder="上传apk" class="layui-input layui-upload-img" id="demo1">
                        </div>
                        <button class="layui-btn up" style="border-radius:5px;margin-left:10px;margin-top:-5px"><input type="file" name="apk_upload" class="test1">上传apk</button>
                    </div>
                </div>
                <div class="layui-form-item" style="width:500px">
                    <label class="layui-form-label" style="text-align:center">version</label>
                    <div class="layui-input-block">
                        <input type="text" name="schoolname" lay-verify="schoolname" autocomplete="off" placeholder="请输入版本" class="layui-input version">
                    </div>
                </div>
                <div class="layui-form-item" style="width:500px">
                    <label class="layui-form-label" style="text-align:center">描&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;述</label>
                    <div class="layui-input-block">
                        <input type="text" name="schoolcode" lay-verify="schoolcode" autocomplete="off" placeholder="请输入描述" class="layui-input msg">
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button class="layui-btn submit site-demo-active"style="border-radius:5px" data-type="tabChange">添加</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{{--<input type="hidden" name="schoolname" lay-verify="schoolname" autocomplete="off" placeholder="请输入类型" class="layui-input type" value="face_TZH-L1">--}}
<input type="hidden" class="classid" value="">
<input type="hidden" class="device">
<input type="hidden" class="classname" value="">

<div id="BOX" style="display:none">
</div>

<script src="{{asset('/layuiadmin/layui/layui.js')}}"></script>
<script>
    var token = localStorage.getItem("Usertoken");

    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        formSelects: 'formSelects'
    }).use(['index', 'form','upload','formSelects','laydate'], function(){
        var $ = layui.$
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form
            ,upload = layui.upload
            ,formSelects = layui.formSelects;

        formSelects.render('store');
        formSelects.btns('store', []);
        var arr=[];
        formSelects.config('store', {
            beforeSuccess: function(id, url, searchVal, result){
                //我要把数据外层的code, msg, data去掉
                result = result.data;
//                console.log(result);
                for(var i=0;i<result.length;i++){
                    var data ={"value":result[i].type,"name":result[i].type_desc};
                    arr.push(data);
                    // console.log(arr);
                }
//                console.log(arr);
                //然后返回数据
                return arr;
            }
        }).data('store', 'server', {
            url:"{{url('/api/user/banner_type?token=')}}"+token
        });
        var arra=[];
        var arrb=[];
        var optionStr='';
        formSelects.on('store', function(id, vals, val, isAdd, isDisabled){
            var optionStr='';
//            console.log(val.value);
            if(isAdd==true){
                arra.push(val.value);
                arrb.push(val.name);
                // console.log(arra.join());
                // console.log(arrb.join());
                $('.classid').val(arra.join());
                $('.classname').val(arrb.join());

                optionStr += '<div class="s_box" class_id="'+$('.classid').val()+'"></div>';
                $("#BOX").append(optionStr);
            }else{
                $('#BOX .s_box').each(function(index,item){
                    if(val.value==$(item).attr('class_id')){
                        $(this).remove();
                    }
                })
            }

        });

        // form.on('select(type)', function(data){
        //     category = data.value;
        //     // categoryName = data.elem[data.elem.selectedIndex].text;
        //     // $('.agentid').val(category);
        //     console.log(category);
        //     $('.type').val(category);
        // });

        form.on('select(type)', function(data){
            device = data.value;
            categoryName = data.elem[data.elem.selectedIndex].text;
            // console.log(category);
            $('.device').val(device);
            // $('.agentname').val(categoryName);
        });

        var uploadInst = upload.render({
            url : "{{url('/api/apk/apkUpload')}}",  //提交到的地址 可以自定义其他参数
            data:{
                token:token
            },
            elem : '.test1',  //指定元素的选择器，默认直接查找class为layui-upload-file的元素
            method : 'POST',    //设置http类型，如：post、get。默认post。也可以直接在input设置lay-method="get"来取代。
            accept : 'file',    //[images 图片类型，默认][file普通文件类型][video视频文件类型][audio音频文件类型]
            exts : 'zip|rar|7z|txt|apk',    //自定义支持的文件格式
            unwrap : true, //是否不改变input的样式风格。默认false
            // size : 5120,
            before : function(input){
                //执行上传前的回调  可以判断文件后缀等等
                layer.msg('上传中，请稍后......', {icon:16, shade:0.5, time:0});
            },
            done: function(res){
//                console.log(res);
                if(res.status == 0){
                    layer.msg(res.msg, {icon:2, shade:0.5, time:res.time});
                }else{
                    layer.msg("文件上传成功", {icon:1, shade:0.5, time:res.time});
                    // layui.jquery('#demo1').attr("src", res.data.img_url);
                    $('#demo1').val(res.data.apk_url);
                }
                //console.log(res); //上传成功返回值，必须为json格式
            }
        });

        upload.render({
            elem: '.test11', //绑定元素
            url: "{{url('/api/apk/apkUpload')}}", //上传接口
            data:{
                token:token
            },
            accept:"file",
            method:"post",
            exts : 'zip|rar|7z|txt|apk',
            before : function(input){
                //执行上传前的回调  可以判断文件后缀等等
                layer.msg('上传中，请稍后......', {icon:16, shade:0.5, time:0});
            },
            done: function(res){
                if(res.status == 0){
                    layer.msg(res.msg, {icon:2, shade:0.5, time:res.time});
                }else{
                    layer.msg("文件上传成功", {icon:1, shade:0.5, time:res.time});
                    // layui.jquery('#demo1').attr("src", res.data.img_url);
                    $('#demo1').val(res.path);
                }
                //console.log(res); //上传成功返回值，必须为json格式
            }
        });
{{--        upload.render({--}}
{{--            url : "{{url('/api/apk/apkUpload')}}" //提交到的地址 可以自定义其他参数--}}
{{--            ,elem: '.test1'--}}
{{--            ,method : 'POST'--}}
{{--            ,type : 'file'--}}
{{--            ,accept: 'apk|text' //视频--}}
{{--            ,before: function(obj){--}}
{{--                //预读本地文件示例，不支持ie8--}}
{{--                layer.msg('上传中，请稍后......', {icon:16, shade:0.5, time:0});--}}
{{--                obj.preview(function(index, file, result){--}}
{{--                    // console.log(result)--}}
{{--                    $("#demo2 .img_box2").each(function(){--}}
{{--                        var index = $(this).index()+1;--}}
{{--                        $(this).attr('data',index);--}}
{{--                    })--}}
{{--                });--}}
{{--            }--}}
{{--            ,done: function(res){--}}
{{--//                console.log(res);--}}
{{--                layer.msg("文件上传成功", {icon:1, shade:0.5, time:res.time});--}}
{{--                $("#demo2 .img_box2 video").find('video').attr('src',res.data.img_url);--}}
{{--                $('#demo2').append('<div class="img_box2" data=""><video src="'+ res.data.img_url +'" controls="controls" class="layui-upload-img"></video><span>×</span><input type="text" class="layui-input url" placeholder="点击视频跳转链接"></div>')--}}
{{--            }--}}
{{--        });--}}

        $('.submit').on('click', function(){
            $.post("{{url('/api/apk/apkCreate')}}",
                {
                    token:token,
                    type:$('.device').val(),
                    update_url:$('#demo1').val(),
                    version:$('.version').val(),
                    msg:$('.msg').val()
                },function(res){
        //                console.log(res);
                    if(res.status==1){
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 1
                            ,time: 1000
                        });
                    }else{
                        layer.msg(res.message, {
                            offset: '50px'
                            ,icon: 2
                            ,time: 1000
                        });
                    }
                },"json");
        });



        $('.submit1111').on('click', function(){
            if($('#tongguo').is(':checked')) {
                $.post("{{url('/api/user/add_banners')}}",
                    {
                        token:token,
                        type:$('.classid').val(),
                        type_desc:$('.classname').val(),
                        title:$('.title').val(),
                        img_url:$('#demo1').val(),
                        banner_time_s:$('.start-item').val(),
                        banner_time_e:$('.end-item').val(),
                        action_url:$('.url').val(),
                        sort:$('.sort').val(),
                        status:'1',
                        banner_desc:$('.desc').val()
                    },function(res){
//                    console.log(res);
                        if(res.status==1){
                            layer.msg(res.message, {
                                offset: '50px'
                                ,icon: 1
                                ,time: 3000
                            });
                        }else{
                            layer.msg(res.message, {
                                offset: '50px'
                                ,icon: 2
                                ,time: 3000
                            });
                        }
                    },"json");
            }else{
                $.post("{{url('/api/user/add_banners')}}",
                    {
                        token:token,
                        type:$('.classid').val(),
                        type_desc:$('.classname').val(),
                        title:$('.title').val(),
                        img_url:$('#demo1').val(),
                        banner_time_s:$('.start-item').val(),
                        banner_time_e:$('.end-item').val(),
                        action_url:$('.url').val(),
                        sort:$('.sort').val(),
                        status:'2',
                        banner_desc:$('.desc').val()
                    },function(res){
//                    console.log(res);
                        if(res.status==1){
                            layer.msg(res.message, {
                                offset: '50px'
                                ,icon: 1
                                ,time: 3000
                            });
                        }else{
                            layer.msg(res.message, {
                                offset: '50px'
                                ,icon: 2
                                ,time: 3000
                            });
                        }
                    },"json");
            }
        });
        laydate.render({
            elem: '.start-item'
            ,type: 'datetime'
            ,done: function(value){
            }
        });
        laydate.render({
            elem: '.end-item'
            ,type: 'datetime'
            ,done: function(value){
            }
        });

    });

</script>

</body>
</html>

